<?php

namespace App\Http\Controllers\Api\Dream\User;


use App\Api\Helpers\ApiResponse;
use App\Api\Helpers\Upload\Single;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class EditController extends Controller
{
    use  Single;
    use  ApiResponse;

    //头像上传 测试  可以使用在后台管理系统
    public function uploadAvatar(Request $request)
    {
        $uploaddir = 'app'.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.time();


        $path = storage_path($uploaddir);

        dd($path);

        $file  = $request->file('avatar');
        $path_url = $this->save($file,'avatar');//单张上传
        return $this->success($path_url);
    }


    //edit  获取个人信息
    public function editUserMsg()
    {

        //根据token获取用户

        $user = Auth::guard('api')->user();//token认证 获取用户信息

        //设置默认头像 ： 如果没有设置头像
        if(!$user->avatar){

            $url = 'user2-160x160.jpg';

            $user->avatar = config('app.url').'/' . $url;

            return $this->success($user);
        }
        return $this->success($user);
    }

    /**
     * @param Request $request
     * @return mixed
     *
     * 编辑个人资料 - 更新个人信息 ：
     * 因为微信小程序的个人信息需要逐条编辑更新，所以需要分别使用接口
     * eg:  头像，昵称，性别，身份证号码，手机号码
     */

    //小程序头像上传
    public function uploadAvatarWx(Request $request)
    {


        $id = $request->id;
        $user = User::where('id',$id)->first();
        $count = User::where('id',$id)->count();
        if (empty($count)){
            return '没有此用户';
        }

        if(!empty($_FILES['avatar'])){
            Log::info($_FILES["avatar"]["type"]."---".$_FILES["avatar"]["name"]."---".$_FILES["avatar"]["size"]);

            //对应要存储的目录
            $uploaddir = 'app'.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'avatars'.DIRECTORY_SEPARATOR.time();

            $uploadfile = $uploaddir . basename($_FILES['avatar']['name']);

            Log::info( $_FILES['avatar']['name'].'/this');

            if(!$_FILES['avatar']['name']){
                return '图片内容为空,上传失败';
            }

            if (move_uploaded_file($_FILES['avatar']['tmp_name'], storage_path($uploadfile))) {
//                Log::info( "File is valid, and was successfully uploaded.\n");

                //eg:  avatars/imag3b7dee9197e0f134fd691c2fad5ed969.jpg
                $path = substr($uploadfile,19);
                $image_url = config('app.url') . "/storage/avatars/".$path;

                $update = User::where('id',$id)->update([
                    'avatar' => $image_url
                ]);
                return $this->success($image_url);
            } else {
                Log::info(  "Possible file wxMini avatar upload attack error!\n");
            }

        }else{
//            Log::info($_FILES['avatar']);
            return 'this wxMini avatar upload error';
        }

    }

    //更新昵称
    public function uploadName(Request $request)
    {
        $user = Auth::guard('api')->user();//token认证 获取用户信息
        $id = $user->id;
        $data_name = $request->name;
        if (empty($data_name)){
            return ' 昵称信息更新失败';
        }else{
            $name=User::where('id',$id)->update([
                'name'=>$data_name
            ]);

            return $this->success($name);
        }

    }

    //更新性别
    public function uploadSex(Request $request)
    {
        $user = Auth::guard('api')->user();//token认证 获取用户信息
        $id = $user->id;
        $data_sex = $request->sex;
        if (empty($data_sex)){
            return ' 性别信息更新失败';
        }else{
            $sex=User::where('id',$id)->update([
                'sex'=>$data_sex
            ]);

            return $this->success($sex);
        }
    }

    //更新身份证号码
    public function uploadIdentity(Request $request)
    {
        $user = Auth::guard('api')->user();//token认证 获取用户信息
        $id = $user->id;
        $data_identity = $request->identity;
        if (empty($data_identity)){
            return ' 身份证信息更新失败';
        }else{
            $identity=User::where('id',$id)->update([
                'identity'=>$data_identity
            ]);

            return $this->success($identity);
        }
    }

    //更新手机号码
    public function uploadPhone(Request $request)
    {
        $user = Auth::guard('api')->user();//token认证 获取用户信息
        $id = $user->id;
        $data_phone = $request->phone;
        if (empty($data_phone)){
            return ' 手机信息更新失败';
        }else{
            $phone=User::where('id',$id)->update([
                'phone'=>$data_phone
            ]);

            return $this->success($phone);
        }
    }


    //更新个人资料  暂不使用
//    public function updateMsg(Request $request)
//    {
//        $user = Auth::guard('api')->user();
//
//        $user_id = $user->id;//获取用户对应ID编号
//
//        $user_update = User::where('id',$user_id)->update([
//            'name' => $request->name,
//            'avatar' => $request->avatar,
//            'phone' => $request->phone,
//            'identity' => $request->identity,
//            'sex' => $request->sex,//性别 1:男 ，2:女
//        ]);
//
//        return $this->success($user_update);
//
//    }


}
